package com.weblog.user.dao;

import com.weblog.model.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.Map;

public interface UserDao extends JpaRepository<User,String> , JpaSpecificationExecutor<User> {

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    User findAllById(String id);

    /**
     * 根据登录名查询用户
     * @param loginName
     * @return
     */
    User findByLoginname(String loginName);


    @Modifying
    @Query(value = "UPDATE tb_user SET isblack = 1 WHERE id=?1",nativeQuery = true)
    int blackUserById(String id);

    /**
     * 根据登录名查询用户
     * @param loginname
     * @return
     */
    User findAllByLoginname(String loginname);

    /**
     * 根据登录名查询用户且id不等于参数中的id
     * @param loginname
     * @param id
     * @return
     */
    User findAllByLoginnameAndIdNot(String loginname, String id);

    /**
     * 用户个人资料中查询展示详细信息
     * @param id
     * @return
     */
    @Query(value = "select new map(mobile as mobile,gender as gender,username as username,regisdate as regisdate,email as email,birthday as birthday,fansnum as fansnum,follownum as follownum) from tb_user where id =?1")
    Map<String,Object> userInfoDetail(String id);

    /**
     * 根据id查询用户名
     * @param id
     * @return
     */
    @Query(value = "select username from tb_user where id=?1",nativeQuery = true)
    String findUsernameById(String id);

    /**
     * 根据id查询用户名和头像
     * @param id
     * @return
     */
    @Query(value = "select username,avatar from tb_user where id=?1",nativeQuery = true)
    String findUserNameAndAvatarById(String id);

    /**
     * 校验手机号是否已被注册
     * @param mobile
     * @return
     */
    User findUserByMobile(String mobile);

    /**
     * 修改密码
     * @param loginname
     * @param newPwd
     */
    @Query(value = "update tb_user set password =?2 where  loginname=?1",nativeQuery = true)
    @Modifying
    void updatePwd(String loginname, String newPwd);

    /**
     * 修改/上传头像
     * @param avatar
     * @param id
     */
    @Query(value = "update tb_user set avatar =?2 where  id=?1",nativeQuery = true)
    @Modifying
    void updateAvatar(String id, String avatar);

    @Query(value = "select avatar from tb_user where id =?1",nativeQuery = true)
    String findAvatarById(String id);

    @Query(value = "update tb_user set score = score +?2 where id =?1",nativeQuery = true)
    @Modifying
    void incrScore(String id, int score);

    @Query(value = "update tb_user set score = ?2 where id =?1",nativeQuery = true)
    @Modifying
    void updateScore(String id, int score);

    /**
     * 前台修改个人资料
     * @param username
     * @param gender
     * @param mobile
     * @param birthday
     * @param email
     * @param id
     */
    @Query(value = "update tb_user set username=?1,gender=?2,mobile=?3,birthday=?4,email=?5 where id=?6 ",nativeQuery = true)
    @Modifying
    void updateInfo(String username, String gender, String mobile, Date birthday, String email,String id);
}
